from typing import *


class Solution:

    def secondGreaterElement(self, nums: List[int]) -> List[int]:
        q1, q2 = [], []
        ans = [-1] * len(nums)
        for i in range(len(nums)):
            while len(q2) > 0 and nums[i] > nums[q2[-1]]:
                ans[q2.pop()] = nums[i]
            t = []
            while len(q1) > 0 and nums[i] > nums[q1[-1]]:
                t.append(q1.pop())
            while t:
                q2.append(t.pop())
            q1.append(i)
        return ans
so = Solution()
print(so.secondGreaterElement([11,13,15,12,0,15,12,11,9]))

# 